using System;
using System.Collections.Generic;
using System.Text;
using Jbe.TestSuite.TestDevice.FunctionGen.Interface.Services;

namespace Jbe.TestSuite.TestDevice.FunctionGen.Driver.Services
{
    public class FunctionGenConfig : IFunctionGenConfig
    {
        private bool readOnly = false;
        private WaveForm waveForm;
        private double amplitude;
        private double frequency;

        public bool IsReadOnly
        {
            get { return readOnly; }
        }
        
        public WaveForm WaveForm
        {
            get { return waveForm; }
            set 
            {
                if (readOnly) { throw new InvalidOperationException("Object is readonly."); }
                waveForm = value; 
            }
        }

        public double Amplitude
        {
            get { return amplitude; }
            set 
            {
                if (readOnly) { throw new InvalidOperationException("Object is readonly."); }
                if (value < 0 || value > 1e3) 
                { 
                    throw new ArgumentOutOfRangeException("Amplitude", value, "The amplitude must be between 0 and 1000 V."); 
                }
                amplitude = value; 
            }
        }

        public double Frequency
        {
            get { return frequency; }
            set 
            {
                if (readOnly) { throw new InvalidOperationException("Object is readonly."); }
                if (value < 0.5 || value > 5e6)
                {
                    throw new ArgumentOutOfRangeException("Frequency", value, "The frequency must be between 0.5 and 5 000 000 Hz.");
                }
                frequency = value; 
            }
        }

        public void Lock()
        {
            readOnly = true;
        }

        public IFunctionGenConfig Clone()
        {
            FunctionGenConfig config = new FunctionGenConfig();
            config.readOnly = false;
            config.waveForm = waveForm;
            config.amplitude = amplitude;
            config.frequency = frequency;
            return config;
        }

        public override bool Equals(object obj)
        {
            FunctionGenConfig config = obj as FunctionGenConfig;
            if (config == null) { return false; }

            return waveForm.Equals(config.waveForm) && amplitude.Equals(config.amplitude) 
                && frequency.Equals(config.frequency);
        }

        public override int GetHashCode()
        {
            return waveForm.GetHashCode() ^ amplitude.GetHashCode() ^ frequency.GetHashCode();
        }
    }
}
